/*
 * Controllers.fx
 *
 * Zawiera elementu layoutu wyswietlane zawsze i wszedzie (procz okien)
 *
 * Created on 2009-08-31, 14:11:51
 */

/**
 * @author m.deluga, jaccus, simet
 */

package galleryviewer;

import javafx.scene.Group;
import javafx.scene.CustomNode;
import javafx.scene.Node;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.image.Image;
import javafx.ext.swing.SwingTextField;
import javafx.scene.text.*;
import javafx.ext.swing.SwingButton;

import javafx.scene.paint.Color;
import galleryviewer.data.DataLoader;



import javafx.scene.image.ImageView;






// ICONS:

  def iconInfo = Image {
              url: "{__DIR__}img/info.png"
              backgroundLoading: true
                    }

   def iconFood = Image {
              url: "{__DIR__}img/food.png"
              backgroundLoading: true
                    }

   def iconToilet = Image {
              url: "{__DIR__}img/toilet.png"
              backgroundLoading: true
                    }

   def iconClothes = Image {
              url: "{__DIR__}img/clothes.png"
              backgroundLoading: true
                    }

   def iconElectronics = Image {
              url: "{__DIR__}img/electronics.png"
              backgroundLoading: true
                    }

   def iconMarket = Image {
              url: "{__DIR__}img/market.png"
              backgroundLoading: true
                    }
                   
    def iconSearch = Image {
            url: "{__DIR__}img/szukaj.png"
            backgroundLoading: true
        }

    def iconList = Image {
            url: "{__DIR__}img/list.png"
            backgroundLoading: true
        }

    def keys = Image {
            url: "{__DIR__}img/keys.png"
            backgroundLoading: true
        }
        
// Icons list
def names = ["food","clothes","electronic","market","toilet"];
def icons = [iconFood, iconClothes, iconElectronics, iconMarket, iconToilet];
def iconColors = [Color.web("#a0b754"), Color.web("#7392cd"), Color.web("#e66034"), Color.web("#b570ad"), Color.web("#717171")];

package var selectedFilter : String = null;

public var currentHighlightColor : Color;

def input = SwingTextField{width: 400 height: 80 font: Font {size: 30} translateY: 20 translateX: 140};

function showSearchedLocals(){
        selectedFilter = input.text;
        Main.currentlyOpenedWindow.close();
        for (local in Local.allLocals) {
            local.refreshColor();      
        }
        searchText = input.text;    
}

var noLocalFoundInfo = Text{
        content: "Nie znaleziono lokali pasujacych do kryterium"
        font: Font { size: 20 embolden: true }
        fill: Color.web("#ee0000")
        translateY: 160
        translateX: 160

    };

var performSearch = function(e:MouseEvent):Void{
    var foundSearchWordInLocalKeywords : Boolean = false;
    Controllers.currentHighlightColor = Color.web("#8f8f8f");
    for(local in Local.allLocals){
        var localBean = DataLoader.getLocal(local.localId);
        for(keyword in localBean.getKeywords()){
            if(input.text.equals(keyword)){
                showSearchedLocals();
                return;
            }
        }
        
    }
    insert noLocalFoundInfo into Main.currentlyOpenedWindow.tresc.content;
    
};

var performSearchButton = SwingButton{translateY: 110 translateX: 300 text: "Szukaj" onMouseClicked : performSearch
                                                                                }

def searchWindowContent : Group = Group{

             content: [

                Text{
                    content: "Czego chcesz szukać?" font: Font { size: 30 }
                    textAlignment: TextAlignment.CENTER
                    translateX: 230
                },

                input,
                performSearchButton,
                
                ImageView {
                        image: keys
                        translateY: 180
                }

        
    ]


    translateX : (Constants.FRAME_WIDTH/2 - 400);
    translateY : 35//(Constants.FRAME_HEIGHT/2 - 200);
};

var displaySearchWindow = function(e:MouseEvent):Void {
        println("{searchText} {selectedFilter}");
        delete noLocalFoundInfo from Main.currentlyOpenedWindow.tresc.content;
        if(searchText!="none"){
            selectedFilter = null;
            searchText = "none";
            for (local in Local.allLocals) {
                local.refreshColor();      
            }//for    
            return;            
        }//if
        
    var localWindow = Window{ tresc: searchWindowContent,jakDojscButton: false };
    insert localWindow into Main.stage.scene.content;
    if( Main.currentlyOpenedWindow != null)
        Main.currentlyOpenedWindow.close();
        
    Main.globalCounter.playFromStart();
    Effects.fadeIn(localWindow);   
         
    Main.currentlyOpenedWindow = localWindow;
};
        
var searchText : String = "none";
public var searchButton = ControlButton{
    image: iconSearch
    name: bind searchText;
    onMouseClicked: displaySearchWindow
};

var showLocalList = function (e:MouseEvent):Void{
    var localWindow = WindowLista{ };
    insert localWindow into Main.stage.scene.content;
    if( Main.currentlyOpenedWindow != null)
        Main.currentlyOpenedWindow.close();
    
    Main.globalCounter.playFromStart();
    Effects.fadeIn(localWindow);        
    Main.currentlyOpenedWindow = localWindow;        
}

public var listButton = ControlButton{
    image: iconList
    name: "none"
    onMouseClicked: showLocalList
}


package class Controllers extends CustomNode {
	public var buttons : ControlButton[];
         
	public override function create(): Node {
		var i : Integer = 0;
		buttons = [
			for (icon in icons) {
				println(names[i]);
				ControlButton {
				image: icon
				name : names[i];
				highlightColor : iconColors[i++];
			}},searchButton,listButton

		];
		return Group {
			content: [
				HBox{ 
					spacing: 20
					content: buttons
				} // HBox
			] // content
		}; // group
	} //Node
}
